﻿function disableBtn(btnID, newText) {
    var btn = document.getElementById(btnID);
    var oldValue = btn.value;
    btn.disabled = true;
    btn.value = newText;
    
    //if validator control present
    if (HasPageValidators()) {
        
        //http://sandblogaspnet.blogspot.com/2009/04/calling-validator-controls-from.html
        //double check, if validator not null
        if (Page_Validators != undefined && Page_Validators != null) {
            //Looping through the whole validation collection.
            for (var i = 0; i < Page_Validators.length; i++) {

                var validator = Page_Validators[i]; 
                
                //check if control to validate is enable 
                if (EnableValidator(validator)) {
                    if (!Page_Validators[i].isvalid) { //if not valid
                        ResetToDefault(btn, oldValue); break;
                    }
                    else { //if valid

                        var isValidationOk = Page_IsValid; 
                        EnableOnUnload(btn, btn.value);
                        if (isValidationOk !== null) {
                            if (isValidationOk) {
                                SetImage(btn);
                                __doPostBack(btnID, '');
                                break;
                            }
                            else { //page not valid
                                btn.disabled = false;
                            }
                        }
                    }
                }    
                
            }
        }
    }
    else { //regular, no validation control present
        SetImage(btn);
        btn.disabled = true; btn.value = newText;
        EnableOnUnload(btn, btn.value);
    }
}

function ResetToDefault(btn, oldValue) {
    btn.disabled = false;
    btn.value = oldValue;
}

//Handle Page_Validators is not defined error
//http://www.velocityreviews.com/forums/t88987-pagevalidators-error.html
function HasPageValidators() {
    var hasValidators = false;
    try {
        if (Page_Validators.length > 0) {
            hasValidators = true;
        }
    }
    catch (error){}
    
    return hasValidators;
}

function SetImage(btn) {
    if (btn.type == "image") {
        
        btn.src = '';
        btn.src = 'http://images.ysatech.com/ajax-loader.gif';
    }
    else {
        btn.style.background = 'url(12501270608.gif)';
    }
}

//enable the button and restore the original text value for browsers other than IE
function EnableOnUnload(btn, btnText) {
    if (navigator.appName !== 'Microsoft Internet Explorer') {
        window.onunload = function() {
            ResetToDefault(btn, btnText);
        }
    }
}

function EnableValidator(validator) {
    var controlToValidate = document.getElementById(validator.controltovalidate);
    if (controlToValidate != null) {
        ValidatorEnable(validator);
        return true;
    }

    return false;
}